package com.jiaai.cloud.system.controller;


import com.jiaai.cloud.base.exception.ServiceException;
import com.jiaai.cloud.base.util.RestResultGenerator;
import com.jiaai.cloud.system.entity.TExchange;
import com.jiaai.cloud.system.service.ITExchangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 佳爱网络科技
 * @since 2017-08-28
 */
@RestController
@RequestMapping("/api/manage/tExchange")
public class TExchangeController {
    @Autowired
    private ITExchangeService itExchangeService;

    //查所有
    @RequestMapping(value ="/findByAll",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object findByAll() throws ServiceException {
        try {
            return RestResultGenerator.genSuccessResult(itExchangeService.selectList(null));
        } catch (Exception e) {
            throw new ServiceException("查询失败", e.getMessage());
        }
    }
    //    删除
    @RequestMapping(value = "/deleteId/{id}", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public Object deleteId(@PathVariable("id") Integer id) throws ServiceException {
        try {

            itExchangeService.deleteById(id);
            return RestResultGenerator.genSuccessResult();
        } catch (Exception e) {
            throw new ServiceException("删除失敗", e.getMessage());
        }
    }
    //改(有就改，没有就插入)
    @RequestMapping(value="/edit",  method=RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    public Object edit(@RequestBody TExchange tExchange)throws ServiceException{
        try {
            tExchange.setCreatTime(new Date());
            itExchangeService.insertOrUpdate(tExchange);
            return null;
        } catch (Exception e) {
            throw new  ServiceException("编辑失敗",e.getMessage());
        }
    }

    //通过改anchorId改status状态
    @RequestMapping(value = "/changeStatus/{id}", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object changeStatus(@PathVariable("id") Integer id) throws ServiceException {
        try {
            TExchange tExchange = new TExchange();
            tExchange.setId(id);
            itExchangeService.updateById(tExchange);
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e) {
            throw new ServiceException("修改失敗", e.getMessage());
        }
    }
	
}
